import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { TypeService } from './type.service';
import {removeSummaryDuplicates} from "@angular/compiler";
import { Response} from '@angular/http';
import {User} from "../../user/model/user-model";
import {GlobalService} from "../../global.service";

@Component({
  selector: 'app-type',
  templateUrl: './type.component.html',
  styleUrls: ['./type.component.css']
})
export class TypeComponent implements OnInit {


  data:Data=new Data;
  draggable:boolean=false;
  addProblemSetFlag:boolean=false;
  focusCount:number=0;
  focusCode:any[] = [];
  type:number;
  selections:Selection[] = [];
  reData:Data[] = [];
  problemSetNames :string[] = [];
  problemSets : ProblemSet[] = [];
  problemSetCode:string;
  pageNumber:number;
  total:number=0;
  detailFlag:boolean=false;
  detailData:Data=new Data;

  currentUser:User;


  constructor(
    public location:Location,
    private typeService:TypeService,
    private globalService:GlobalService
  ) { }

  ngOnInit() {
    let url = this.location.path();
    this.type = parseInt(url.substring(url.lastIndexOf("/")+1));
    this.data.properties.degree='';
    this.search(null);
    let _this = this;

    this.currentUser = this.globalService.getCurrentUser();

    let queryData = {
      properties:new Properties()
    };
    queryData.properties.creator = this.currentUser.code;
    this.typeService.findProblemSetList(queryData).subscribe((res:Response)=>{
      _this.problemSets = [];
      let response = res.json();
      if(response.success){
        response.rows.forEach(function (item) {
          _this.problemSets.push({
            name:item.name,
            code:item.code
          });
        });
      }

      if(_this.problemSets.length>0){
        _this.problemSetCode = _this.problemSets[0].code;
      }
    });
  }

  focus(event){
    this.selections[event.target.alt].flag = event.target.checked;
    if(event.target.checked){
      this.focusCount++;
    }else{
      this.focusCount--;
    }
  }
  focusAll(event){
    this.focusCount = 0;
    for(let i=0;i<this.selections.length;i++){
      this.selections[i].flag = event.target.checked;
      if(this.selections[i].flag){
        this.focusCount++;
      }
    }
  }
  addProblemSetPanelShow(){
    this.addProblemSetFlag = true;
  }
  addProblemSet(){
    let _this = this;
    console.log("problemSetCode:",this.problemSetCode);
    console.log("selections:",this.selections);

    let problems:Selection[] = [];
    this.selections.forEach(function (item) {
      if(item.flag){
        problems.push(item);
      }
    });
    this.typeService.addProblemsToSet({
      code:this.problemSetCode,
      problems:problems
    }).subscribe((res:Response)=>{
      if(res.json().success){
        _this.addProblemSetFlag = false;
      }else{
        alert(res.json().msg);
      }
    });

  }
  search(event){
    let _this = this;
    if(this.pageNumber==null||(event&&event.target.name=='search')){
      this.pageNumber = 0;
    }
    this.data.properties.pageNumber = this.pageNumber;
    this.typeService.findProblemInfos(this.data).subscribe((res:Response)=>{
      _this.reData = [];
      _this.selections = [];
      let xx = res.json();
      _this.total = res.json().total;
      console.log(xx);
      xx.rows.forEach(function (item) {
        _this.selections.push({
          code:item.code,
          flag:false
        });
        _this.reData.push({
          code:item.code,
          name:item.name,
          author:item.author,
          company:'',
          describeHtml:item.describeHtml,
          creator:item.creator,
          degree:item.degree,
          score:item.score,
          updateTime:item.updateTime,
          properties:new Properties,
        });
      });
    });
  }
  paginate(event) {

    console.log(event);

    //event.first = Index of the first record
    //event.rows = Number of rows to display in new page
    //event.page = Index of the new page
    //event.pageCount = Total number of pages
    this.pageNumber = event.page;
    this.search(null);
  }
  detail(event){

    console.log(this.reData);
    console.log(event);

    let code = event.target.name;
    for(let i=0;i<this.reData.length;i++){
      if(code==this.reData[i].code){
        this.detailData = this.reData[i];
        this.detailFlag = true;
        break;
      }
    }
  }
  hideDetail(){
    this.detailFlag = false;
  }
}

class Data{
  code:any;
  name:any;
  author:any;
  creator:any;
  company:any;
  describeHtml:any;
  degree:number;score:number;
  updateTime:any;
  properties:Properties = new Properties;
}
class Properties{
  name_like:any;
  degree:any;
  pageNumber:number=0;
  pageSize:number=10;
  creator:string;
}
class Selection{
  flag:boolean;
  code:string;
}
class ProblemSet{
  name:string;
  code:string;
}
class Problem{
  code:string;
}
